<template>
  <div class="upload-container">
    <el-button
      :style="{ background: color, borderColor: color }"
      size="mini"
      type="primary"
      class="bj_color"
      @click="dialogVisible = true"
    >
      插入图片
    </el-button>
    <el-dialog :visible.sync="dialogVisible" center append-to-body>
      <!-- <el-upload
        :multiple="true"
        :file-list="fileList"
        :show-file-list="true"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
        class="editor-slide-upload"
        action="https://httpbin.org/post"
        list-type="picture-card"
      >
        <el-button size="small" type="primary"> 点击上传图片 </el-button>
      </el-upload> -->

      <div class="logoDiv">
        <div class="divs" v-for="(item, index) in imgList" :key="index">
          <div class="imageDiv">
            <div class="zzc"></div>
            <!-- <i
              class="el-icon-zoom-in look"
              @click="[(dialogImageUrl = item), (dialogVisible = true)]"
            ></i> -->
            <i
              class="el-icon-delete delete"
              @click="imgList.splice(index, 1)"
            ></i>
            <img class="image" :src="item" />
          </div>
        </div>
        <div>
          <el-upload
            action
            :http-request="uploadImgList"
            :show-file-list="false"
            multiple
          >
            <img
              class="uplod_Logo"
              src="@/assets/images/upload_img.png" 
              alt=""
            />
          </el-upload>
        </div>
      </div>

      <span slot="footer" class="dialog-footer ty_tc_btn">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="handleSubmit"> 确认 </el-button>
      </span>
    </el-dialog>

    
  </div>
</template>

<script>
// import { getToken } from 'api/qiniu'
import upload  from '@/common/upload_img';
export default {
  name: "EditorSlideUpload",
  props: {
    color: {
      type: String,
      default: "#1890ff",
    },
  },
  data() {
    return {
      dialogVisible: false,
      listObj: {},
      fileList: [],
      //
      imgList: [],
      dialogVisible: false,
      dialogImageUrl: null,
    };
  }, 
  methods: {

    async uploadImgList(file) {
      let obj = await new upload().uploadImg(file, this);
      if (obj.type == true) {
        this.imgList.push(obj.url);
        console.log(this.imgList)
        // this.$message.success("上传成功");
      } else {
        this.$message.warning("上传失败");
      }
    },

    checkAllSuccess() {
      return Object.keys(this.listObj).every(
        (item) => this.listObj[item].hasSuccess
      );
    },
    handleSubmit() {
      // const arr = Object.keys(this.listObj).map((v) => this.listObj[v]);
      // if (!this.checkAllSuccess()) {
      //   this.$message(
      //     "Please wait for all images to be uploaded successfully. If there is a network problem, please refresh the page and upload again!"
      //   );
      //   return;
      // }
      // this.$emit("successCBK", arr);
      // this.listObj = {};
      // this.fileList = [];
      // this.dialogVisible = false;

      //<!--     -->
      if(this.imgList.length==0){
        this.$$message.warning('至少上传一张图片');
        return;
      }
      const arr = this.imgList.map(item=>Object.assign({url:item}))
      this.$emit("successCBK", arr);
      this.imgList =[];
      this.dialogVisible = false;
    },
    handleSuccess(response, file) {
      const uid = file.uid;
      const objKeyArr = Object.keys(this.listObj);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.files.file;
          this.listObj[objKeyArr[i]].hasSuccess = true;
          return;
        }
      }
    },
    handleRemove(file) {
      const uid = file.uid;
      const objKeyArr = Object.keys(this.listObj);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]];
          return;
        }
      }
    },
    beforeUpload(file) {
      const _self = this;
      const _URL = window.URL || window.webkitURL;
      const fileName = file.uid;
      this.listObj[fileName] = {};
      return new Promise((resolve, reject) => {
        const img = new Image();
        img.src = _URL.createObjectURL(file);
        img.onload = function () {
          _self.listObj[fileName] = {
            hasSuccess: false,
            uid: file.uid,
            width: this.width,
            height: this.height,
          };
        };
        resolve(true);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  ::v-deep .el-upload--picture-card {
    width: 100%;
  }
}

.logoDiv{
  width:100%;
  justify-content:center;
}
.delete{
  transform: translate(-50%, -50%) !important;
}
</style>